package com.example.ndktest;

import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SimpleActivity extends AppCompatActivity {

    @BindView(R.id.tv_original_var)
    TextView tvOriginalVar;
    @BindView(R.id.tv_modified_var)
    TextView tvModifiedVar;
    @BindView(R.id.tv_original_static_var)
    TextView tvOriginalStaticVar;
    @BindView(R.id.tv_modified_static_var)
    TextView tvModifiedStaticVar;
    @BindView(R.id.tv_access_static_method_result)
    TextView tvAccessStaticMethodResult;
    @BindView(R.id.tv_access_method_result)
    TextView tvAccessMethodResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.bind(this);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("简单调用");
        }

        tvOriginalVar.setText("Variable:" + JNIHelper.getInstance().s);
        tvOriginalStaticVar.setText("Static Variable:" + JNIHelper.count);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.btn_modify_var, R.id.btn_modify_static_var, R.id.btn_access_static_method,
            R.id.btn_access_method})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_modify_var:
                JNIHelper.getInstance().accessField();
                new Handler().postDelayed(() -> tvModifiedVar.setText("Modified Variable:" + JNIHelper.getInstance().s), 1000);
                break;
            case R.id.btn_modify_static_var:
                JNIHelper.getInstance().accessStaticField();
                new Handler().postDelayed(() -> tvModifiedStaticVar.setText("Modified Static Variable:" + JNIHelper.count), 1000);
                break;
            case R.id.btn_access_static_method:
                tvAccessStaticMethodResult.setText("Access Static Method Result:" + JNIHelper.getInstance().accessStaticMethod());
                break;
            case R.id.btn_access_method:
                tvAccessMethodResult.setText("Access Method Result:" + JNIHelper.getInstance().accessMethod());
                break;
        }
    }
}
